package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.TestBean;

/**
 * 待办工作列表适配器
 * 支持两种类型：促销活动审批 和 打卡流程审批
 * 
 * 通过 TestBean.type 字段区分类型：
 * type = "1" → 促销活动审批
 * type = "2" → 打卡流程审批
 */
public class PendingWorkAdapter extends AppAdapter<TestBean> {

    private static final int TYPE_PROMOTION = 1; // 促销活动审批
    private static final int TYPE_CLOCK_IN = 2;  // 打卡流程审批

    private OnButtonClickListener mButtonClickListener;

    public PendingWorkAdapter(Context context) {
        super(context);
    }

    /**
     * 设置按钮点击监听
     */
    public void setOnButtonClickListener(OnButtonClickListener listener) {
        mButtonClickListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        // 根据 TestBean.type 字段判断类型
        TestBean item = getItem(position);
        if ("2".equals(item.getType())) {
            return TYPE_CLOCK_IN;
        }
        return TYPE_PROMOTION;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_CLOCK_IN) {
            return new ClockInViewHolder();
        } else {
            return new PromotionViewHolder();
        }
    }

    /**
     * 促销活动审批 ViewHolder（复用 approval_application_item）
     */
    private final class PromotionViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mTitleView;
        private final TextView mStatusView;
        private final TextView mActivityNameView;
        private final TextView mOrderDateView;
        private final TextView mMainProductScopeView;
        private final TextView mGiftScopeView;
        private final TextView mCustomerScopeView;

        private PromotionViewHolder() {
            super(R.layout.approval_application_item);
            mTitleView = findViewById(R.id.tv_title);
            mStatusView = findViewById(R.id.tv_status);
            mActivityNameView = findViewById(R.id.tv_activity_name);
            mOrderDateView = findViewById(R.id.tv_order_date);
            mMainProductScopeView = findViewById(R.id.tv_main_product_scope);
            mGiftScopeView = findViewById(R.id.tv_gift_scope);
            mCustomerScopeView = findViewById(R.id.tv_customer_scope);
        }

        @Override
        public void onBindView(int position) {
            TestBean item = getItem(position);
            
            // 使用 TestBean 的 name 作为标题和活动名称
            mTitleView.setText("买赠活动");
            mActivityNameView.setText(item.getName());
            mOrderDateView.setText("2025/8/18—2025/10/18");
            mMainProductScopeView.setText("指定商品（买赠）/全部商品（满赠）");
            mGiftScopeView.setText("指定商品");
            mCustomerScopeView.setText("部分终端/配送商/销售区域");
            
            // 待办工作固定显示"待审批"状态
            mStatusView.setText("待审批");
            mStatusView.setBackgroundResource(R.drawable.shape_red_2_kong);
            mStatusView.setTextColor(getColor(R.color.red_app));
        }
    }

    /**
     * 打卡流程审批 ViewHolder
     */
    private final class ClockInViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mTitleView;
        private final TextView mTimeView;
        private final TextView mReasonView;
        private final TextView mClockInTimeView;
        private final TextView mRejectInitiatorBtn;
        private final TextView mRejectPreviousBtn;
        private final TextView mApproveBtn;

        private ClockInViewHolder() {
            super(R.layout.clock_in_approval_item);
            mTitleView = findViewById(R.id.tv_title);
            mTimeView = findViewById(R.id.tv_time);
            mReasonView = findViewById(R.id.tv_reason);
            mClockInTimeView = findViewById(R.id.tv_clock_in_time);
            mRejectInitiatorBtn = findViewById(R.id.tv_reject_initiator);
            mRejectPreviousBtn = findViewById(R.id.tv_reject_previous);
            mApproveBtn = findViewById(R.id.tv_approve);

            // 设置按钮点击事件
            mRejectInitiatorBtn.setOnClickListener(v -> {
                if (mButtonClickListener != null) {
                    int position = getViewHolderPosition();
                    TestBean item = getItem(position);
                    mButtonClickListener.onRejectToInitiator(position, item);
                }
            });

            mRejectPreviousBtn.setOnClickListener(v -> {
                if (mButtonClickListener != null) {
                    int position = getViewHolderPosition();
                    TestBean item = getItem(position);
                    mButtonClickListener.onRejectToPrevious(position, item);
                }
            });

            mApproveBtn.setOnClickListener(v -> {
                if (mButtonClickListener != null) {
                    int position = getViewHolderPosition();
                    TestBean item = getItem(position);
                    mButtonClickListener.onApprove(position, item);
                }
            });
        }

        @Override
        public void onBindView(int position) {
            TestBean item = getItem(position);
            // 使用 TestBean 的 name 作为标题
            mTitleView.setText(item.getName());
            mTimeView.setText("2025/10/12 16:40");
            mReasonView.setText("有项目需签订");
            mClockInTimeView.setText("2025/8/27 07:00");
        }
    }

    /**
     * 按钮点击监听接口
     */
    public interface OnButtonClickListener {
        /**
         * 驳回发起人
         */
        void onRejectToInitiator(int position, TestBean item);

        /**
         * 驳回上一级
         */
        void onRejectToPrevious(int position, TestBean item);

        /**
         * 审核通过
         */
        void onApprove(int position, TestBean item);
    }
}

